package com.chuanke.family.face;

import com.chuanke.ckfamily.dao.user.model.User;
import com.chuanke.ckfamily.service.common.RSAService;
import com.chuanke.ckfamily.service.common.TokenService;
import com.chuanke.ckfamily.service.user.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.chuanke.ckfamily.service.base.FailCode;
import com.chuanke.ckfamily.service.exception.CFException;
import com.chuanke.ckfamily.service.face.FaceService;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@RequestMapping("/api/face")
@Controller
public class FaceController {

    private static final Logger logger = LoggerFactory.getLogger(FaceController.class);

    @Autowired
    private FaceService faceService;

    @Autowired
    private UserService userService;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private RSAService rsaService;

    /**
     * <h1>用户登录获取token接口</h1>
     * /api/face/login_token
     *
     * @param sign 验签
     * @param uid  用户id
     * @return data为token
     */
    @ResponseBody
    @RequestMapping("/login_token")
    public Response getLoginToken(HttpServletRequest request, @RequestParam("sign") String sign, @RequestParam("uid") Long uid) {
        try {
            logger.info("login token = {},sign={}", uid, sign);
            // 解密
            faceService.checkSign(request, sign);
            User user = userService.findById(uid);
            logger.info("create token for user={}", user.getId());
            String token = tokenService.createToken(user, true, true);

            User update = new User();
            update.setId(user.getId());
            update.setToken(token);
            userService.update(update);

            userService.checkUserFans(uid);
            return new Response(token);

        } catch (CFException e) {
            e.printStackTrace();
            return new Response(e);

        } catch (Exception e) {
            e.printStackTrace();
            return new Response(FailCode.APP, e.getMessage());

        }
    }

    /**
     * <h1>获取公钥</h1>
     * /api/face/get_publickey
     *
     * @return
     */
    @RequestMapping("/get_publickey")
    @ResponseBody
    public String getPublicKey() {
        return rsaService.getPublicKey();
    }

    /**
     * 用户活跃
     * /api/face/user_active
     *
     * @param uid
     * @param shareSize
     * @return
     */
    @RequestMapping("/user_active")
    @ResponseBody
    public Response userActivity(HttpServletRequest request, @RequestParam("sign") String sign, @RequestParam("uid") Long uid, @RequestParam("share_size") Long shareSize) {
        try {
            logger.info("user active ===========> sign={} uid={} share_size={}", sign, uid, shareSize);
            // 解密
            faceService.checkSign(request, sign);
            faceService.userActivity(uid, shareSize);
            return new Response("活跃度更新成功");

        } catch (CFException e) {
            e.printStackTrace();
            return new Response(e);

        } catch (Exception e) {
            e.printStackTrace();
            return new Response(FailCode.APP, e.getMessage());
        }
    }

    /**
     * 用户更新
     * /api/face/update_user_info
     *
     * @param uid
     * @return
     */
    @RequestMapping("/update_user_info")
    @ResponseBody
    public Response userUpdate(@RequestParam("uid") Long uid) {
        try {
            faceService.userUpdate(uid);
            return new Response("ok");

        } catch (CFException e) {
            e.printStackTrace();
            return new Response(e);

        } catch (Exception e) {
            e.printStackTrace();
            return new Response(FailCode.APP, e.getMessage());
        }
    }

    /**
     * 用户注册
     * /api/face/user_register
     *
     * @param uid
     * @return
     */
    @RequestMapping("/user_register")
    @ResponseBody
    public Response userRegist(HttpServletRequest request, @RequestParam("sign") String sign,
                               @RequestParam("uid") Long uid, @RequestParam("elect_id") Long elect_id) {
        try {
            logger.debug("user register ===========> sign={} uid={} elect_id={}", sign, uid, elect_id);
            // 解密
            faceService.checkSign(request, sign);
            faceService.userRegist(uid, elect_id);
            return new Response("ok");

        } catch (CFException e) {
            e.printStackTrace();
            return new Response(e);

        } catch (Exception e) {
            e.printStackTrace();
            return new Response(FailCode.APP, e.getMessage());

        }
    }

    @RequestMapping("/update_price_interface")
    @ResponseBody
    public Response updatePriceInterface(@RequestParam("flag") Integer flag) {
        return new Response("成功");
    }
}
